import { ref, Ref } from 'vue'
import { getPersonalized, getPersonalizedNewsong, getPlayListDetail } from '@/apis/index'
import { formatNumber } from '@/utils/utils'

//推荐歌单逻辑
export async function useHandleAlbum() {
  const albumList: Ref<CustomTypeAlubm.Album[]> = ref([])
  //获取歌单列表
  const res = await getPersonalized({ limit: 6 })
  albumList.value = res.result.map((item) => {
    return {
      pic: item.picUrl,  //专辑图片
      title: item.name, //专辑名称
      count: formatNumber(item.playCount), //专辑播放量
      ...item
    }
  })
  return {
    albumList
  }
}

//最新音乐逻辑
export async function useHanleSongList() {
  const res = await getPersonalizedNewsong({ limit: 20 })
  const songList: Ref<any[]> = ref([])
  songList.value = res.result.map(item => {
    const subTitle: string = item.song.album.artists.map(i => i.name).join('、') + item.song.album.name
    return {
      title: item.name,
      subTitle,
      free: true,//item.song.fee == 8,
      isSq: item.song.privilege.playMaxbr >= 999000,
      id: item.id,
      url: `https://music.163.com/song/media/outer/url?id=${item.id}.mp3`
    }
  })
  // console.log(songList)
  return {
    songList
  }
}


//热歌榜
export async function useHotSongs(id) {
  const playList = ref([])
  const album = ref(null)
  const res = await getPlayListDetail({ id })
  album.value = res.playlist
  playList.value = res.playlist.tracks.map((item, index) => {
    const subTitle: string = item.ar.map(i => i.name).join('、') + item.al.name
    return {
      title: item.name,
      free: true,//item.fee == 8,
      subTitle,
      isSq: res.privileges[index].playMaxbr >= 999000,
      id: item.id,
      url: `https://music.163.com/song/media/outer/url?id=${item.id}.mp3`
    }
  })
  return {
    playList: playList,
    album
  }
}